package com.example.blog_system_ssm.controller;

import com.example.blog_system_ssm.component.*;
import com.example.blog_system_ssm.entity.UserEntity;
import com.example.blog_system_ssm.service.MailService;
import com.example.blog_system_ssm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.websocket.Session;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;


    @Autowired
    private CheckCodeTools codeTools;  // 生成验证码的工具对象

    // 获取图片验证码
    @RequestMapping("/getImageVerificationCode")
    public AjaxResult getCode(HttpServletRequest request) {
        // 生成图片验证码
        String[] codeArr = codeTools.createImage();
        // 将验证码存储到 session 里
        HttpSession session = request.getSession();
        session.setAttribute(ApplicationVariable.SESSION_KEY_CHECK_CODE, codeArr[1]);
        return AjaxResult.success("/image/" + codeArr[0]);
    }

    // 注册用户功能
    @RequestMapping("/register")
    public AjaxResult createAccount(UserEntity userEntity){
        // 前端已经进行非空校验，后端需要再进行非空校验么？ 解答：一定要，因可以使用非正常的方式进行测试后端，如postman测试工具。
        // 1,进行非空校验
        if(userEntity == null || !StringUtils.hasLength(userEntity.getUsername())
                || !StringUtils.hasLength(userEntity.getPassword()) || !StringUtils.hasLength(userEntity.getEmail())){
            return AjaxResult.fail(-1,"参数有误！");
        }
        UserEntity user = userService.selectByName(userEntity.getUsername());
        if(user != null)
            return AjaxResult.fail(-2,"用户名已存在！");

        return userService.createAccount(userEntity);
    }

    // 用户登录功能
    @RequestMapping("/login")
    public AjaxResult loginAccount(String username, String password,String code, HttpServletRequest request){
        if(!StringUtils.hasLength(username) || !StringUtils.hasLength(password) || !StringUtils.hasLength(code))
            return AjaxResult.fail(-1,"参数有误！");
        UserEntity userEntity = userService.loginAccount(username);
        if(userEntity == null || userEntity.getId() <= 0 ){
            return AjaxResult.fail(-2,"用户名错误！");
        }
        if(userEntity.getState() == 0){
            return AjaxResult.fail(-3,"用户未激活！");
        }
        // 将密码进行验证操作
        if(!PasswordTools.decrypt(password,userEntity.getPassword()))
            return AjaxResult.fail(-2,"密码错误！");

        // 判断图片验证码是否正确
        HttpSession session = request.getSession(false);
        String imageCode = (String) session.getAttribute(ApplicationVariable.SESSION_KEY_CHECK_CODE);
        if(!code.equals(imageCode))
            return AjaxResult.fail(-4,"图片验证码错误或者过期！");

        // 将当前成功登录的用户信息存储到Session中
        HttpSession session1 = request.getSession(true);
        session1.setAttribute(ApplicationVariable.SESSION_KEY_USERINFO,userEntity);
        return AjaxResult.success(1);
    }


    // 用户账号激活功能
    @RequestMapping("/activation")
    public AjaxResult activationAccount(@RequestParam("activationCode") String confirmCode, String username){
        if(!StringUtils.hasLength(confirmCode)){
            return AjaxResult.fail(-1,"激活码参数为空！");
        }
        if(!StringUtils.hasLength(username)){
            return AjaxResult.fail(-1,"用户名参数为空！");
        }
        return userService.activationAccount(confirmCode,username);
    }

//    // 用户账号激活功能
//    @RequestMapping("/activation")
//    public AjaxResult activationAccount(@RequestParam("activationCode") String confirmCode){
//        return userService.activationAccount(confirmCode);
//    }


    // 注销用户登录功能
    @RequestMapping("/logout")
    public AjaxResult logout(HttpServletRequest request){
        HttpSession session = request.getSession();
        // 移除 Session
        session.removeAttribute(ApplicationVariable.SESSION_KEY_USERINFO);
        return AjaxResult.success(1);
    }

    // 判断当前用户是否登录
    @RequestMapping("/islogin")
    public AjaxResult isLogin(HttpServletRequest request){
        if(UserSessionTools.getLoginUser1(request) == null){
            // 未登录
            return AjaxResult.success(0);
        }
        return AjaxResult.success(1);
    }


}
